#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License,  Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http: //www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,  software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,  either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import urllib2
import simplejson as json
import os
from google.appengine.ext.webapp import template
import datetime
from google.appengine.ext import db
import sys
from BLL import BusinessLogicLayer

FB_API_ID = '218658404835737'
FB_API_KEY = '644c3da3218c6be2fbceaae04b68de4f'
FB_SECRET = '2ba92929c23ca51f10bec6b444c73b91'
FB_API_URL = 'http://apps.facebook.com/guildturkey/'
FB_API_AUTH_URL = 'https://www.facebook.com/dialog/oauth?client_id=' + FB_API_ID + '&redirect_uri=' + FB_API_URL
###https://edwinsturkey.appspot.com/


class MainPage(webapp.RequestHandler):

    def post(self):
        self._BLL = BusinessLogicLayer()
        try:
            code = self.request.get('code')
            if code:
                data = self._BLL.fetch_user_full_name(FB_API_ID,
                                                                FB_API_URL,
                                                                FB_SECRET,
                                                                code)
                template_values = {
                'fb_user': data,
                }
                path = os.path.join(os.path.dirname(__file__), 'UIL/index.html')
                self.response.out.write(template.render(path, template_values))
    #            self.response.out.write(path)
    #            fb_user_id = BLL_.fetch_fb_user_id(self.request, FB_SECRET)
    #            if  fb_user_id:
            else:
                self.get()
        except:
            print "Unexpected error: ", sys.exc_info()[0]
            raise

    def get(self):
        write = '<script>top.location.href="%s"</script>' % (FB_API_AUTH_URL)
        self.response.out.write(write)


class GuestBook(webapp.RequestHandler):

    def post(self):
        _greetings = self.request.get('content')

        template_values = {
                'greetings': _greetings,
                'url': 'http://www.google.be',
                'url_linktext': 'HIT ME!',
        }
        path = os.path.join(os.path.dirname(__file__), 'UIL/guestbook.html')
        self.response.out.write(template.render(path, template_values))





application = webapp.WSGIApplication([('/', MainPage),('/sign', GuestBook)], debug=True)


def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
